package com.lxx.jmb2c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxx.jmb2c.dto.GoodsPageQueryDTO;
import com.lxx.jmb2c.entity.Brand;
import com.lxx.jmb2c.entity.Category;
import com.lxx.jmb2c.entity.Goods;
import com.lxx.jmb2c.mapper.GoodsMapper;
import com.lxx.jmb2c.service.BrandService;
import com.lxx.jmb2c.service.CategoryService;
import com.lxx.jmb2c.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxx.jmb2c.vo.GoodsDetailVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.lxx.jmb2c.vo.GoodsPageVO;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2025-07-10
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private CategoryService categoryService;

    @Resource
    private BrandService brandService;

    @Override
    public IPage<GoodsPageVO> getPage(GoodsPageQueryDTO goodsPageQueryDTO) {
        IPage<Goods> pageInfo=new Page<>(goodsPageQueryDTO.getCurrent(), goodsPageQueryDTO.getPageSize());
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(goodsPageQueryDTO.getGoodsNo()), Goods::getGoodsNo, goodsPageQueryDTO.getGoodsNo());
        wrapper.eq(StringUtils.hasText(goodsPageQueryDTO.getGoodsNo()), Goods::getGoodsNo, goodsPageQueryDTO.getGoodsNo());
        wrapper.eq(goodsPageQueryDTO.getCategoryId() != null, Goods::getCategoryId, goodsPageQueryDTO.getCategoryId());
        wrapper.eq(goodsPageQueryDTO.getBrandId() != null, Goods::getBrandId, goodsPageQueryDTO.getBrandId());
        IPage<Goods> page = this.page(pageInfo, wrapper);

        IPage<GoodsPageVO> pageVO = new Page<>();

        //数据对拷 -> 拷贝页面规格信息，当前第几页，每页多少条数据，总条数，总页数
        BeanUtils.copyProperties(page, pageVO);
        // 替换records
        List<Goods> goodsList = page.getRecords();

        //创建列表用来接收vo对象
        // pageVO是刚刚new出来的对象，里面的records实际是null,所以goodsPageVOList.add(goodsPageVO)本质是null.add(goodsPageVO)
        // 所以会抛出空指针异常
        //List<GoodsPageVO> goodsPageVOList = pageVO.getRecords();
        List<GoodsPageVO> goodsPageVOList = new ArrayList<>();

        for (Goods goods : goodsList) {
            //entity->vo
            GoodsPageVO goodsPageVO = new GoodsPageVO();
            BeanUtils.copyProperties(goods, goodsPageVO);
            //补充其他属性
            Category category = categoryService.getById(goods.getCategoryId());
            goodsPageVO.setCategoryName(category.getName());

            Brand brand = brandService.getById(goods.getBrandId());
            goodsPageVO.setBrandName(brand.getName());

            goodsPageVOList.add(goodsPageVO);
        }

        //替换pageVO的records
        pageVO.setRecords(goodsPageVOList);
        return pageVO;
    }

    @Override
    public GoodsDetailVO getGoodsDetailById(Long id) {
        Goods goods = this.getById(id);
        //goods->goodsDetailVO
        GoodsDetailVO goodsDetailVO = new GoodsDetailVO();
        //属性对拷
        BeanUtils.copyProperties(goods, goodsDetailVO);
        //补充其他属性
        Category category = categoryService.getById(goods.getCategoryId());
        goodsDetailVO.setCategoryName(category.getName());

        Brand brand = brandService.getById(goods.getBrandId());
        goodsDetailVO.setBrandName(brand.getName());

        return goodsDetailVO;
    }
}
